#导入类库
import numpy as np
from numpy import arange
from matplotlib import pyplot
from pandas import read_csv
from pandas.plotting import scatter_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.metrics import mean_squared_error

#导入数据
filename = 'housing.csv'
names = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE',
         'DIS','RAD', 'TAX', 'PRTATIO', 'B', 'LSTAT', 'MEDV']
data = read_csv(filename, names=names, delim_whitespace=True)
# print(data.shape)
#
# print(data.dtypes)
#
# print(data.head(30))
#
# print(data.describe())
#
# print(data.corr(method='pearson'))

#数据的可视化
# data.hist(sharex=False, sharey=False, xlabelsize=1, ylabelsize=1)
# pyplot.show()
# data.plot(kind='density', subplots=True, layout=(4, 4), sharex=False, fontsize=1)
# pyplot.show()
#
# data.plot(kind='box', subplots=True, layout=(4, 4),
#           sharex=False, sharey=False, fontsize=8)
# pyplot.show()

#散点图
# scatter_matrix(data)
# pyplot.show()
#矩阵图
# fig = pyplot.figure()
# ax = fig.add_subplot(111)
# cax = ax.matshow(data.corr(), vmin=-1, vmax=1, interpolation='none')
# fig.colorbar(cax)
# ticks = np.arange(0, 14, 1)
# ax.set_xticks(ticks)
# ax.set_yticks(ticks)
# ax.set_xticklabels(names)
# ax.set_yticklabels(names)
# pyplot.show()

#特征选择， 标准化数据， 正太化数据
#数据分离
array = data.values
X =  array[:, 0:13]
Y = array[:, 13]
validation_size = 0.2
seed = 7
X_train, X_validation, Y_train, Y_validation =train_test_split(X, Y,
                                                               test_size=validation_size,
                                                               random_state=seed)

#评估算法
num_folds = 10
seed = 7
scoring = 'neg_mean_squared_error'
models = {}
models['LR'] = LinearRegression()
models['LASSO'] = Lasso()
models['EN'] = ElasticNet()
models['KNN'] = KNeighborsRegressor()
models['CART'] = DecisionTreeRegressor()
models['SVM'] = SVR()
# results = []
# for key in models:
#     kfold = KFold(n_splits=num_folds, random_state=seed, shuffle=True)
#     cv_result = cross_val_score(models[key], X_train, Y_train, cv=kfold, scoring=scoring)
#     results.append(cv_result)
#     print('%s: %f(%f)' %(key, cv_result.mean(), cv_result.std()))
#
# fig = pyplot.figure()
# fig.suptitle('Algorithm Comparision')
# ax = fig.add_subplot(111)
# pyplot.boxplot(results)
# ax.set_xticklabels(models.keys())
# pyplot.show()




# 评估算法 - 正态化数据
# pipelines = {}
# pipelines['ScalerLR'] = Pipeline([('Scaler', StandardScaler()), ('LR', LinearRegression())])
# pipelines['ScalerLASSO'] = Pipeline([('Scaler', StandardScaler()), ('LASSO', Lasso())])
# pipelines['ScalerEN'] = Pipeline([('Scaler', StandardScaler()), ('EN', ElasticNet())])
# pipelines['ScalerKNN'] = Pipeline([('Scaler', StandardScaler()), ('KNN', KNeighborsRegressor())])
# pipelines['ScalerCART'] = Pipeline([('Scaler', StandardScaler()), ('CART', DecisionTreeRegressor())])
# pipelines['ScalerSVM'] = Pipeline([('Scaler', StandardScaler()), ('SVM', SVR())])
# results = []
# for key in pipelines:
#     kfold = KFold(n_splits=num_folds, random_state=seed, shuffle=True)
#     cv_result = cross_val_score(pipelines[key], X_train, Y_train, cv=kfold, scoring=scoring)
#     results.append(cv_result)
#     print('%s: %f (%f)' % (key, cv_result.mean(), cv_result.std()))
#评估算法 - 箱线图
# fig = pyplot.figure()
# fig.suptitle('Algorithm Comparison')
# ax = fig.add_subplot(111)
# pyplot.boxplot(results)
# ax.set_xticklabels(models.keys())
# pyplot.show()

#调参
# scaler = StandardScaler().fit(X_train)
# rescalerX = scaler.transform(X_train)

# # rescaler = StandardScaler().fit_transform(X_train)
# para_grid = {'n_neighbores': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]}

# model = KNeighborsRegressor()
# kfold = KFold(n_splits=num_folds, random_state=seed, shuffle=True)
# grid = GridSearchCV(estimator=model, param_grid=para_grid, scoring=scoring, cv=kfold)
# grid_result = grid.fit(X=rescalerX, y=Y_train)
# # print('最优： %s 使用%s' %(grid_result.best_score_, grid_result.best_params_))
# # cv_results = zip(grid_result.cv_results_['mean_test_score'],
# #                  grid_result.cv_results_['std_test_score'],
# #                  grid_result.cv_results_['params'])
# # for mean, std, param in cv_results:
# #     print('%f(%f) with %r' %(mean, std, param))

scaler = StandardScaler().fit(X_train)
rescaledX = scaler.transform(X_train)
param_grid = {'n_neighbors': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]}
model = KNeighborsRegressor()
kfold = KFold(n_splits=num_folds, random_state=seed, shuffle=True)
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring=scoring, cv=kfold)
grid_result = grid.fit(X=rescaledX, y=Y_train)

print('最优：%s 使用%s' % (grid_result.best_score_, grid_result.best_params_))
cv_results = zip(grid_result.cv_results_['mean_test_score'],
                 grid_result.cv_results_['std_test_score'],
                 grid_result.cv_results_['params'])
for mean, std, param in cv_results:
    print('%f (%f) with %r' % (mean, std, param))


#集成算法
ensembles = {}
ensembles['ScaledAB'] = Pipeline([('Scaler', StandardScaler()), ('AB', AdaBoostRegressor())])
ensembles['ScaledAB-KNN'] = Pipeline([('Scaler', StandardScaler()),
                                      ('ABKNN', AdaBoostRegressor(base_estimator=KNeighborsRegressor(n_neighbors=1)))])
ensembles['ScaledAB-LR'] = Pipeline([('Scaler', StandardScaler()),
                                     ('ABLR', AdaBoostRegressor(LinearRegression()))])
ensembles['ScaledRFR'] = Pipeline([('Scaler', StandardScaler()),
                                   ('RFR', RandomForestRegressor())])
ensembles['ScaledETR'] = Pipeline([('Scaler', StandardScaler()),
                                   ('ETR', ExtraTreesRegressor())])
ensembles['ScaledGBR'] = Pipeline([('Scaler', StandardScaler()),
                                  ('RBR', GradientBoostingRegressor())])
results = []
for key in ensembles:
    kfold = KFold(n_splits=num_folds, random_state=seed, shuffle=True)
    cv_result = cross_val_score(ensembles[key], X_train, Y_train, cv=kfold, scoring=scoring)
    results.append(cv_result)
    print('%s: %f (%f)' % (key, cv_result.mean(), cv_result.std()))
















